﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SiemensOPC;
using Opc.Ua;
using Opc.Ua.Client;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using System.Data;
using VswService_V7.Utility;

namespace VswService_V7.OPC.DB
{
	public class QCAddressDbdata : SubscriptionDbDataBase
	{
		public QCAddressDbdata(SimaticOPCClient opc, NodeConfig nodes) : base(opc, nodes)
		{
            _pipe = new BasicPipeInfo();

        }

        BasicPipeInfo _pipe;
        string _Location = "QC";

		public override void CreateSubscription()
		{
			opcServer.QuickAddMonitorItem(opcNodes.QCAddress.GrpoupName, 
                opcNodes.QCAddress.PublishTime,
                opcNodes.QCAddress.SampleTime,
				opcNodes.QCAddress.GetNodeAddress(), 
                opcNodes.QCAddress.GetNodeDisplayName(),
				SubscriptionHandleQC);
			opcServer.QuickAddMonitorItem(opcNodes.QCCurrentPipeAddress.GrpoupName, 
                opcNodes.QCCurrentPipeAddress.PublishTime, 
                opcNodes.QCCurrentPipeAddress.SampleTime,
				opcNodes.QCCurrentPipeAddress.GetNodeAddress(),
                opcNodes.QCCurrentPipeAddress.GetNodeDisplayName(),
				SubscriptionHandleQCCurrent);
		}

		private void SubscriptionHandleQC(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
		{
			MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
			if (notification == null)
				return;
			if (monitoredItem.DisplayName == "QC_Request")
			{
				bool ask;
				if (bool.TryParse(notification.Value.Value.ToString(), out ask))
				{
					if (ask)
					{
                        Task.Run(() => HandlePipe());
                    }
					else
					{
						opcServer.WriteValue("false", opcNodes.QCAddress.QC_Response);
					}
				}
			}
		}

		private void SubscriptionHandleQCCurrent(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
		{
			MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
			if (notification == null)
				return;
			switch (monitoredItem.DisplayName)
			{
				case "Batch_No":
					{
                        _pipe.batchNumber = notification.Value.Value.ToString();
						break;
					}
				case "Pipe_No":
					{
						_pipe.pipeNumber = notification.Value.Value.ToString();
						break;
					}
				case "Pipe_Quantity":
					{
                        _pipe.qualityCode = notification.Value.Value.ToString();
                        break;
					}
				default:
					{
						break;
					}
			}
		}

        private async Task HandlePipe()
        {
            await Task.Delay(1000);
            try
            {
                //PIPE_INFO.UpdatePipeInfoRecord(_pipe.GetFullPipeNumber(), DateTime.Now, _Location);
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("HandlePipe", ex);
            }
            
            opcServer.WriteValue("true", opcNodes.QCAddress.QC_Response);
        }
	}
}
